package com.group5.service;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.group5.dao.CompanyDrugInformationMapper;
import com.group5.dao.PurchaseListMapper;
import com.group5.dao.PurchasePlanMapper;
import com.group5.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


public class OrderService {
    @Autowired
    PurchasePlanMapper purchasePlanMapper;
    @Autowired
    CompanyDrugInformationMapper companyDrugInformationMapper;
    @Autowired
    PurchaseListMapper purchaseListMapper;

    /**
     * 卖方查询买方提交的订单
     * @param order_id
     * @return
     */
    public PurchaseListExample sellerQueryPurchaseListByOrderIDService(Integer order_id){
        PurchaseListExample purchaseListExample = new PurchaseListExample();
        purchaseListExample.createCriteria().andOrder_idEqualTo(order_id);
        return purchaseListExample;
    }

    /**
     * 买方核价查询订单
     * @param buyer_oid
     * @return
     */
    public PurchasePlanExample buyerQueryPurchasePlan(Integer buyer_oid,Integer status){
        PurchasePlanExample purchasePlanExample = new PurchasePlanExample();
        purchasePlanExample.createCriteria().andBuyer_oidEqualTo(buyer_oid).andList_statusEqualTo(status);
        return purchasePlanExample;
    }
    public int getSellerOidByObject(Object object) {
        IntegerToJsonService integerToJsonService = JSONArray.parseObject(JSON.toJSONString(object),IntegerToJsonService.class);
        return integerToJsonService.getOid();
    }

    public String getBuyerAdminUsernameByObject(Object object) {
        StringToJsonService.UsernameToJson usernameToJson=JSONArray.parseObject(JSON.toJSONString(object), StringToJsonService.UsernameToJson.class);
        return usernameToJson.getUsername();
    }


    public String getStateByObject(Object object) {
        StringToJsonService.StateToJson stateToJson=JSONArray.parseObject(JSON.toJSONString(object), StringToJsonService.StateToJson.class);
        return stateToJson.getState();
    }


    public PurchaseList getPurchaseListByObject(Object object) {
        PurchaseList purchaseList=JSONArray.parseObject(JSON.toJSONString(object),PurchaseList.class);
        return purchaseList;
    }

    public String getTime(){
        Date date=new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return formatter.format(date);
    }

    public UserssExample selectUserByUsername(String buyerAdmin_username) {
        UserssExample userssExample=new UserssExample();
        userssExample.createCriteria().andUser_nameEqualTo(buyerAdmin_username);
        return userssExample;
    }

    public PurchasePlanExample selectPurchaseBySellerAndBuyerAndOrderTime(int seller_oid, Integer oid, String orderTime) {
        PurchasePlanExample purchasePlanExample=new PurchasePlanExample();
        purchasePlanExample.createCriteria().andSeller_oidEqualTo(seller_oid).andBuyer_oidEqualTo(oid).andOrder_timeEqualTo(orderTime);
        return purchasePlanExample;
    }

    public CompanyDrugInformationExample selectDrugByOidAndDrugid(int seller_oid, Integer drug_id) {
        CompanyDrugInformationExample companyDrugInformationExample = new CompanyDrugInformationExample();
        companyDrugInformationExample.createCriteria().andOid_sellerEqualTo(seller_oid).andDrug_idEqualTo(drug_id);
        return companyDrugInformationExample;
    }

    public CompanyDrugInformationExample buyerQueryOneSellerDrugExample(int oid){
        CompanyDrugInformationExample companyDrugInformationExample = new CompanyDrugInformationExample();
        companyDrugInformationExample.createCriteria().
                andOid_sellerEqualTo(oid).
                andState_of_drugEqualTo("审核通过");
        return companyDrugInformationExample;
    }

    public PurchaseListExample deleteListsByOrder_idExample(Integer order_id) {
        PurchaseListExample purchaseListExample=new PurchaseListExample();
        purchaseListExample.createCriteria().andOrder_idEqualTo(order_id);
        return purchaseListExample;
    }

    public PurchaseListExample selectPurchaseListByOrder_id(Integer order_id) {
        PurchaseListExample purchaseListExample=new PurchaseListExample();
        purchaseListExample.createCriteria().andOrder_idEqualTo(order_id);
        return purchaseListExample;
    }

    public CompanyDrugInformationExample selectCompanyDrugByOidAndDrug_id(Integer seller_oid, Integer drug_id) {
        CompanyDrugInformationExample companyDrugInformationExample=new CompanyDrugInformationExample();
        companyDrugInformationExample.createCriteria().andDrug_idEqualTo(drug_id).andOid_sellerEqualTo(seller_oid);
        return companyDrugInformationExample;
    }
}
